<?php

require_once 'db_config.php';

class DbMgr {

    private $hote = "";
    private $port = "";
    private $db = "";
    private $user = "";
    private $mdp = "";
    public $con = null;

    public function connexion($type) {
        $this->hote = Db_config::$hote;
        $this->port = Db_config::$port;
        $this->db = Db_config::$db;
        $this->user = Db_config::$user;
        $this->mdp = Db_config::$mdp;
        try {
            $this->con = new PDO("$type:host=" . $this->hote . ";dbname=" . $this->db, $this->user, $this->mdp);
            $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	    $this->con->exec('SET NAMES utf8');
        } catch (Exception $e) {
            echo 'Erreur n°' . $e->getCode() . ': ' . $e->getMessage() . '<br />';
            die();
        }
    }

    public function select($select) {
        $res = $this->con->query($select);
        if ($res) {
            //$res->setFetchMode(PDO::FETCH_COL);
            $val = array();
            while ($ligne = $res->fetch(PDO::FETCH_ASSOC)) {
                $val[] = $ligne;
            }
            $res->closeCursor();
            return $val;
        }
    }

    public function insert($query) {
        $nb = $this->con->exec($query);
        return $nb;
    }

    /* public function update()
      {
      }

      public function delete($query)
      {
      } */
}

?>
